package com.example.bingobox.ui.state;

import android.app.Application;
import android.os.Environment;

import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.example.bingobox.model.State;
import com.google.gson.Gson;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class StateViewModel extends AndroidViewModel {
    private final MutableLiveData<State> state;
    private final Gson gson;

    public StateViewModel(Application application) {
        super(application);
        state = new MutableLiveData<>(new State());
        gson = new Gson();
        loadState();
    }

    private void loadState() {
        File dataDir = new File(Environment.getExternalStorageDirectory(), "BingoBox/data");
        File stateFile = new File(dataDir, "state.json");
        
        try (FileReader reader = new FileReader(stateFile)) {
            State loadedState = gson.fromJson(reader, State.class);
            if (loadedState != null) {
                state.setValue(loadedState);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public LiveData<State> getState() {
        return state;
    }
} 